/***************************************************************************
 创建者：华磊
 开始时间：         : 2020.12.13
 copyright            : (C) 深圳市华友高科有限公司
 修改说明：（每次有修改就添加一条，带有 修改人，修改时间，修改描述）

 ***************************************************************************
 *   等待指令属性编辑视窗
*                                                                         *
 ***************************************************************************/
#include "waitinstructionview.h"
#include "ui_waitinstructionview.h"
#include "../../programView/programview.h"


WaitInstructionView::WaitInstructionView(int styleId,QWidget *parent):
    QWidget(parent),
    ui(new Ui::WaitInstructionView)
{
    ui->setupUi(this);
    initialDisplayStyle(styleId);

    for(int i = 1; i <= 200 ; i ++)
    {
        ui->lblIndex_comboBox->addItem(QString::number(i));
    }
}

void WaitInstructionView::initialDisplayStyle(int styleId)
{
    switch (styleId) {
    case 1:
        ui->groupBox->setGeometry(10,0,430,150);
        ui->TIMEOUT_GBox->setGeometry(10,160,430,110);
        ui->note_GBox->setGeometry(10,280,430,65);

        ui->forward_Btn->setGeometry(10,350,70,40);
        ui->save_Btn->setGeometry(360,350,70,40);
        break;
    default:
        break;
    }
}

void WaitInstructionView::updateView()
{
    if(EM_WAIT_INSTRUCTION_TIME == waitType)
    {
        ui->waitTime_radioButton->setChecked(true);       
        ui->waitTime_lineEdit->setText(QString("%1").arg(waitTime));
        ui->TIMEOUT_GBox->setEnabled(false);
    }
    else if(EM_WAIT_INSTRUCTION_R == waitType)
    {
        ui->waitR_radioButton->setChecked(true);
        ui->R_Index_lineEdit->setText(rIndex);
        ui->TIMEOUT_GBox->setEnabled(false);
    }
    else if(EM_WAIT_INSTRUCTION_CONDITION == waitType)
    {
        ui->waitCondition_radioButton->setChecked(true);
        ui->TIMEOUT_GBox->setEnabled(true);

        ui->waitCondition_lineEdit->setText(waitCondition);

        if(0 == timeoutSelectedFlag)
        {
            ui->timeout_checkBox->setChecked(false);
            ui->lblIndexType_comboBox->setEnabled(false);
            ui->lblIndex_comboBox->setEnabled(false);
            ui->limitWaitTime_checkBox->setEnabled(false);
            ui->limitWaitTime_lineEdit->setEnabled(false);
            ui->limitWaitTimeType_comboBox->setEnabled(false);
        }
        else if(1 == timeoutSelectedFlag)
        {
            ui->timeout_checkBox->setChecked(true);
            ui->lblIndexType_comboBox->setEnabled(true);
            ui->lblIndex_comboBox->setEnabled(true);
            ui->lblIndexType_comboBox->setCurrentIndex(timeoutLblIndexType);
            ui->lblIndex_comboBox->setCurrentIndex(timeoutLblIndex - 1);
            ui->limitWaitTime_checkBox->setEnabled(true);
            ui->limitWaitTimeType_comboBox->setEnabled(true);

            if(0 == limitWaitTimeSelectFlag)
            {
                ui->limitWaitTime_checkBox->setChecked(false);
                ui->limitWaitTime_lineEdit->setEnabled(false);
            }
            else if(1 == limitWaitTimeSelectFlag)
            {
                ui->limitWaitTime_checkBox->setChecked(true);
                ui->limitWaitTime_lineEdit->setEnabled(true);
                if(0 == limitWaitTimeType)
                {
                    ui->limitWaitTime_lineEdit->setText(QString("%1").arg(limitWaitTime));
                }
                else if(1 == limitWaitTimeType)
                {
                    ui->limitWaitTime_lineEdit->setText(timeoutRIndex);
                }
            }
        }
    }

    ui->note_lineEdit->setText(note);
}

void WaitInstructionView::getInput()
{
    waitTime = ui->waitTime_lineEdit->text().toFloat();
    rIndex = ui->R_Index_lineEdit->text();
    waitCondition = ui->waitCondition_lineEdit->text();
    note = ui->note_lineEdit->text();
    timeoutLblIndexType = ui->lblIndexType_comboBox->currentIndex();
    timeoutLblIndex = ui->lblIndex_comboBox->currentIndex() + 1;

    //qDebug() << limitWaitTimeType;
    limitWaitTimeType = ui->limitWaitTimeType_comboBox->currentIndex();
    //qDebug() << limitWaitTimeType;
    if(0 == limitWaitTimeType)
    {
        limitWaitTime = ui->limitWaitTime_lineEdit->text().toFloat();
    }
    else if(1 == limitWaitTimeType)
    {
        timeoutRIndex = ui->limitWaitTime_lineEdit->text();
    }
}

void WaitInstructionView::on_waitTime_radioButton_clicked()
{
    emit selectWaitTime();
}

void WaitInstructionView::on_waitR_radioButton_clicked()
{
    emit selectWaitR();
}

void WaitInstructionView::on_waitCondition_radioButton_clicked()
{
    emit selectWaitCondition();
}

void WaitInstructionView::on_clearNote_Btn_clicked()
{
    ui->note_lineEdit->clear();
    emit clearNote();
}

void WaitInstructionView::on_forward_Btn_clicked()
{
    this->close();
    programView->instructionListFlag = 0;
    programView->editInstructionViewShowOrHide();
}

void WaitInstructionView::on_save_Btn_clicked()
{
    getInput();

    emit finishEditInstruction();
    emit saveInstruction(waitInstruction);
}

void WaitInstructionView::on_timeout_checkBox_clicked()
{
    emit isSelectTimeout();
}

void WaitInstructionView::on_limitWaitTime_checkBox_clicked()
{
    emit isSelectSettingLimitTime();
}

void WaitInstructionView::on_limitWaitTimeType_comboBox_currentIndexChanged(int index)
{
    limitWaitTimeType = index;
}

void WaitInstructionView::on_waitCondition_lineEdit_textChanged(const QString &arg1)
{
    waitCondition = arg1;//预先存储
}

void WaitInstructionView::changeLanguage(int language)
{
    if(1 == language)
    {
        translator.load("/agvwork/AgvHmi/baseBag/hmiProject/language/waitViewEnglshLan.qm");
        qApp->installTranslator(&translator);
    }
    else
    {
        qApp->removeTranslator(&translator);
    }
    ui->retranslateUi(this);
}

